package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.GetConfigureInfoUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/24.
 */
@Component("ExportDataCheckScAdminContrastReportSource")
public class ExportDataCheckScAdminContrastReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckScAdminContrastReportSource.class);

    private String manualData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_销售行政_手工台账数据";
    private String systemData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_销售行政_LiVE系统数据";

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[2];
        sheets[0] = new Sheet(manualData,manualData,true);
        sheets[1] = new Sheet(systemData,systemData,true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("startDate") == null ? null : context.get("startDate").toString();
        String endDate = context.get("endDate") == null ? null : context.get("endDate").toString();
        String nowDate = null;
        if (StringUtils.isNullOrBlank(startDate) && StringUtils.isNullOrBlank(endDate)) {
            nowDate = DateFormatUtils.getFormatDateToString(new java.util.Date(), "yyyyMMdd");
        }
        List list = dataCheckRepository.getScAdminContrast(permit, nowDate, startDate, endDate);

        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                if (manualData.equals(sheet.getName())) {
                    values.add(new Row(getCells(map)));
                } else if (systemData.equals(sheet.getName())) {
                    values.add(new Row(getOtherCells(map)));
                }
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[24];
        int index = 0;
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("inStockDt") == null ? "" : map.get("inStockDt").toString(), index, !"1".equals(map.get("dinStockDt") == null ? null : map.get("dinStockDt").toString()));
        cells[index++] = new Cell(map.get("outStockDt") == null ? "" : map.get("outStockDt").toString(), index, !"1".equals(map.get("doutStockDt") == null ? null : map.get("doutStockDt").toString()));
        cells[index++] = new Cell(map.get("reDt") == null ? "" : map.get("reDt").toString(), index, !"1".equals(map.get("dreDt") == null ? null : map.get("dreDt").toString()));
        cells[index++] = new Cell(map.get("orderCusName") == null ? "" : map.get("orderCusName").toString(), index, !"1".equals(map.get("dorderCusName") == null ? null : map.get("dorderCusName").toString()));
        cells[index++] = new Cell(map.get("invoiceUname") == null ? "" : map.get("invoiceUname").toString(), index, !"1".equals(map.get("dinvoiceUname") == null ? null : map.get("dinvoiceUname").toString()));
        cells[index++] = new Cell(map.get("priceTi") == null ? "" : map.get("priceTi").toString(), index, !"1".equals(map.get("dpriceTi") == null ? null : map.get("dpriceTi").toString()));
        cells[index++] = new Cell(map.get("freebyIn") == null ? "" : map.get("freebyIn").toString(), index, !"1".equals(map.get("dfreebyIn") == null ? null : map.get("dfreebyIn").toString()));
        cells[index++] = new Cell(map.get("freebySubInt") == null ? "" : map.get("freebySubInt").toString(), index, !"1".equals(map.get("dfreebySubInt") == null ? null : map.get("dfreebySubInt").toString()));
        cells[index++] = new Cell(map.get("freebyA") == null ? "" : map.get("freebyA").toString(), index, !"1".equals(map.get("dfreebyA") == null ? null : map.get("dfreebyA").toString()));
        cells[index++] = new Cell(map.get("freebyB") == null ? "" : map.get("freebyB").toString(), index, !"1".equals(map.get("dfreebyB") == null ? null : map.get("dfreebyB").toString()));
        cells[index++] = new Cell(map.get("freebySi") == null ? "" : map.get("freebySi").toString(), index, !"1".equals(map.get("dfreebySi") == null ? null : map.get("dfreebySi").toString()));
        cells[index++] = new Cell(map.get("freebyBl") == null ? "" : map.get("freebyBl").toString(), index, !"1".equals(map.get("dfreebyBl") == null ? null : map.get("dfreebyBl").toString()));
        cells[index++] = new Cell(map.get("freebySh") == null ? "" : map.get("freebySh").toString(), index, !"1".equals(map.get("dfreebySh") == null ? null : map.get("dfreebySh").toString()));
        cells[index++] = new Cell(map.get("freebyOi") == null ? "" : map.get("freebyOi").toString(), index, !"1".equals(map.get("dfreebyOi") == null ? null : map.get("dfreebyOi").toString()));
        cells[index++] = new Cell(map.get("freebyIsIn") == null ? "" : map.get("freebyIsIn").toString(), index, !"1".equals(map.get("dfreebyIsIn") == null ? null : map.get("dfreebyIsIn").toString()));
        cells[index++] = new Cell(map.get("freebyIsSssc") == null ? "" : map.get("freebyIsSssc").toString(), index, !"1".equals(map.get("dfreebyIsSssc") == null ? null : map.get("dfreebyIsSssc").toString()));
        cells[index++] = new Cell(map.get("freebyIsSd") == null ? "" : map.get("freebyIsSd").toString(), index, !"1".equals(map.get("dfreebyIsSd") == null ? null : map.get("dfreebyIsSd").toString()));
        cells[index++] = new Cell(map.get("freebyIsOil") == null ? "" : map.get("freebyIsOil").toString(), index, !"1".equals(map.get("dfreebyIsOil") == null ? null : map.get("dfreebyIsOil").toString()));
        cells[index++] = new Cell(map.get("freebyIsPre") == null ? "" : map.get("freebyIsPre").toString(), index, !"1".equals(map.get("dfreebyIsPre") == null ? null : map.get("dfreebyIsPre").toString()));
        cells[index++] = new Cell(map.get("freebyIsTi") == null ? "" : map.get("freebyIsTi").toString(), index, !"1".equals(map.get("dfreebyIsTi") == null ? null : map.get("dfreebyIsTi").toString()));
        cells[index++] = new Cell(map.get("purTaxSi") == null ? "" : map.get("purTaxSi").toString(), index, !"1".equals(map.get("dpurTaxSi") == null ? null : map.get("dpurTaxSi").toString()));
        cells[index++] = new Cell(map.get("reSi") == null ? "" : map.get("reSi").toString(), index, !"1".equals(map.get("dreSi") == null ? null : map.get("dreSi").toString()));
        cells[index++] = new Cell(map.get("whSi") == null ? "" : map.get("whSi").toString(), index, !"1".equals(map.get("dwhSi") == null ? null : map.get("dwhSi").toString()));
        return cells;
    }


    private Cell[] getOtherCells (Map<String, Object> map) {
        Cell[] cells = new Cell[24];
        int index = 0;
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("cinStockDt") == null ? "" : map.get("cinStockDt").toString(), index, !"1".equals(map.get("dinStockDt") == null ? null : map.get("dinStockDt").toString()));
        cells[index++] = new Cell(map.get("coutStockDt") == null ? "" : map.get("coutStockDt").toString(), index, !"1".equals(map.get("doutStockDt") == null ? null : map.get("doutStockDt").toString()));
        cells[index++] = new Cell(map.get("creDt") == null ? "" : map.get("creDt").toString(), index, !"1".equals(map.get("dreDt") == null ? null : map.get("dreDt").toString()));
        cells[index++] = new Cell(map.get("corderCusName") == null ? "" : map.get("corderCusName").toString(), index, !"1".equals(map.get("dorderCusName") == null ? null : map.get("dorderCusName").toString()));
        cells[index++] = new Cell(map.get("cinvoiceUname") == null ? "" : map.get("cinvoiceUname").toString(), index, !"1".equals(map.get("dinvoiceUname") == null ? null : map.get("dinvoiceUname").toString()));
        cells[index++] = new Cell(map.get("cpriceTi") == null ? "" : map.get("cpriceTi").toString(), index, !"1".equals(map.get("dpriceTi") == null ? null : map.get("dpriceTi").toString()));
        cells[index++] = new Cell(map.get("cfreebyIn") == null ? "" : map.get("cfreebyIn").toString(), index, !"1".equals(map.get("dfreebyIn") == null ? null : map.get("dfreebyIn").toString()));
        cells[index++] = new Cell(map.get("cfreebySubInt") == null ? "" : map.get("cfreebySubInt").toString(), index, !"1".equals(map.get("dfreebySubInt") == null ? null : map.get("dfreebySubInt").toString()));
        cells[index++] = new Cell(map.get("cfreebyA") == null ? "" : map.get("cfreebyA").toString(), index, !"1".equals(map.get("dfreebyA") == null ? null : map.get("dfreebyA").toString()));
        cells[index++] = new Cell(map.get("cfreebyB") == null ? "" : map.get("cfreebyB").toString(), index, !"1".equals(map.get("dfreebyB") == null ? null : map.get("dfreebyB").toString()));
        cells[index++] = new Cell(map.get("cfreebySi") == null ? "" : map.get("cfreebySi").toString(), index, !"1".equals(map.get("dfreebySi") == null ? null : map.get("dfreebySi").toString()));
        cells[index++] = new Cell(map.get("cfreebyBl") == null ? "" : map.get("cfreebyBl").toString(), index, !"1".equals(map.get("dfreebyBl") == null ? null : map.get("dfreebyBl").toString()));
        cells[index++] = new Cell(map.get("cfreebySh") == null ? "" : map.get("cfreebySh").toString(), index, !"1".equals(map.get("dfreebySh") == null ? null : map.get("dfreebySh").toString()));
        cells[index++] = new Cell(map.get("cfreebyOi") == null ? "" : map.get("cfreebyOi").toString(), index, !"1".equals(map.get("dfreebyOi") == null ? null : map.get("dfreebyOi").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsIn") == null ? "" : map.get("cfreebyIsIn").toString(), index, !"1".equals(map.get("dfreebyIsIn") == null ? null : map.get("dfreebyIsIn").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsSssc") == null ? "" : map.get("cfreebyIsSssc").toString(), index, !"1".equals(map.get("dfreebyIsSssc") == null ? null : map.get("dfreebyIsSssc").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsSd") == null ? "" : map.get("cfreebyIsSd").toString(), index, !"1".equals(map.get("dfreebyIsSd") == null ? null : map.get("dfreebyIsSd").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsOil") == null ? "" : map.get("cfreebyIsOil").toString(), index, !"1".equals(map.get("dfreebyIsOil") == null ? null : map.get("dfreebyIsOil").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsPre") == null ? "" : map.get("cfreebyIsPre").toString(), index, !"1".equals(map.get("dfreebyIsPre") == null ? null : map.get("dfreebyIsPre").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsTi") == null ? "" : map.get("cfreebyIsTi").toString(), index, !"1".equals(map.get("dfreebyIsTi") == null ? null : map.get("dfreebyIsTi").toString()));
        cells[index++] = new Cell(map.get("cpurTaxSi") == null ? "" : map.get("cpurTaxSi").toString(), index, !"1".equals(map.get("dpurTaxSi") == null ? null : map.get("dpurTaxSi").toString()));
        cells[index++] = new Cell(map.get("creSi") == null ? "" : map.get("creSi").toString(), index, !"1".equals(map.get("dreSi") == null ? null : map.get("dreSi").toString()));
        cells[index++] = new Cell(map.get("cwhSi") == null ? "" : map.get("cwhSi").toString(), index, !"1".equals(map.get("dwhSi") == null ? null : map.get("dwhSi").toString()));
        return cells;
    }
}
